package xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.model;

import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import xixirupan.tmgp.tencent.com.vmeim.config.URLConfig;
import xixirupan.tmgp.tencent.com.vmeim.retrofit.IBottomIndexR;

/**
 * Created by lenovo on 2016/10/20.
 */

public class ShopMainDataBiz {
    private int curPage =1;

 PostMainDataListener listener;
  //设置页码的不同
    public  void getBottomData(final PostMainDataListener listener,int curPage)
    {
        //以参数形式传递过偶来
        this.curPage =curPage;

        this.listener =listener;

        Retrofit retrofit =new Retrofit.Builder().baseUrl(URLConfig.Url.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build();

        IBottomIndexR iBottomIndexR = retrofit.create(IBottomIndexR.class);
        //?pageNo=1
        HashMap<String,String> map =new HashMap<>();
        map.put("pageNo",curPage+"");
        Call<ShopMainBean> call = iBottomIndexR.getMainDat(map);
        call.enqueue(new Callback<ShopMainBean>() {
            @Override
            public void onResponse(Call<ShopMainBean> call, Response<ShopMainBean> response) {
                ShopMainBean body = response.body();

                ShopBottomBean data = body.getData();

                List<Object> lists =new ArrayList<>();

                List<DataListBaen> dataList = data.getDataList();

                for (int i = 0; i < dataList.size(); i++) {
                    lists.add(dataList.get(i).getImgURL());
                    List<ProductsBaen> products = dataList.get(i).getProducts();
                    //数据源中有空
                    if (products!=null&&products.size()>0)
                    {

                        lists.add(products);
                    }
                }
                Log.i("主要数据index中的","_--------->下载成功"+lists.size());

                listener.postData(lists );

            }

            @Override
            public void onFailure(Call<ShopMainBean> call, Throwable t) {
                Log.i("主要数据index中的","_--------->下载失败"+t.getMessage());
            }
        });
    }

    public interface  PostMainDataListener{
         void postData(List<Object> lists);
    }
}
